package com.yl.order.ui.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yl.order.App;
import com.yl.order.R;

import java.util.ArrayList;

/**
 * create by cjx on 2024/6/17
 * desc:
 */
public abstract class BaseEmptyAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_EMPTY = 0;

    // 数据
    protected ArrayList<T> list;

    // 是否显示空布局
    private boolean showEmptyView = false;

    public BaseEmptyAdapter(ArrayList<T> list) {
        this.list = list;
    }

    @Override
    public int getItemViewType(int position) {
        if (list == null || list.isEmpty()) {
            // 空布局
            return TYPE_EMPTY;
        } else {
            return getCustomItemViewType(position);
        }
    }

    @Override
    public int getItemCount() {
        return list == null || list.isEmpty() ? 1 : list.size();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == TYPE_EMPTY) {
            View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_empty, viewGroup, false);
            return new EmptyViewHolder(v);
        } else {
            return createItemViewHolder(viewGroup, i);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        int itemViewType = getItemViewType(i);
        if (itemViewType == TYPE_EMPTY) {
            EmptyViewHolder emptyViewHolder = (EmptyViewHolder) viewHolder;
            emptyViewHolder.tipView.setText(getEmptyTip());
        } else {
            onBindItemHolder(viewHolder, i);
        }
    }

    public CharSequence getEmptyTip() {
        return App.app.getString(R.string.empty_data);
    }

    public abstract void onBindItemHolder( RecyclerView.ViewHolder viewHolder, int i);

    public abstract RecyclerView.ViewHolder createItemViewHolder(ViewGroup parent, int i);

    public int getCustomItemViewType(int position) {
        return -1;
    }

    private static class EmptyViewHolder extends RecyclerView.ViewHolder {
        TextView tipView;

        public EmptyViewHolder(@NonNull View itemView) {
            super(itemView);
            this.tipView = itemView.findViewById(R.id.tip);
        }
    }
}
